package cn.net.mugui.net.p2p;

import com.mugui.bean.JsonBean;
import com.mugui.sql.SQLDB;
import com.mugui.sql.SQLField;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * p2p连接的属性
 *
 * @author 木鬼
 */
@SuppressWarnings("serial")
@Getter()
@Accessors(chain = true)
@SQLDB(TABLE = "server_attr", KEY = "id")
public class ServerAttr extends JsonBean {

    /**
     * 0: 未知类型<br>
     */
    public static final int type_0 = 0;
    /**
     * 1: 公网服务器<br>
     */
    public static final int type_1 = 0x1;
    /**
     * 2: 内网固定host port型<br>
     */
    public static final int type_2 = 0x2;
    /**
     * 3: 内网固定host 动态port型<br>
     */
    public static final int type_3 = 0x3;
    /**
     * 4: 内网可直连
     */
    public static final int type_4 = 0x4;
    /**
     * 0:已断开
     */
    public static final int status_0 = 0;
    /**
     * 1:待连接
     */
    public static final int status_1 = 1;
    /**
     * 2:连接中
     */
    public static final int status_2 = 2;
    /**
     * 3:已连接
     */
    public static final int status_3 = 3;
    public static ServerAttr self = new ServerAttr().setAddress("11111111111111111111111111111111");
    @Getter(AccessLevel.NONE)
    private ServerAttrListener listener;
    @SQLField(PRIMARY_KEY = true, AUTOINCREMENT = true, AUTOINCREMENT_value = "autoincrement")
    private Integer id;
    /**
     * 服务名称
     */
    @SQLField
    private String name;
    /**
     * 服务器所在host
     */
    @SQLField(DEFAULT = true, DEFAULT_text = "0")
    private String host;
    /**
     * 服务器端口
     */
    @SQLField(DEFAULT = true, DEFAULT_text = "0")
    private Integer port;
    
    /**
     * 检测端口
     */
    @Setter
    private Integer check_port;
    /**
     * 服务器类型<br>
     * 0: 未知类型<br>
     * 1: 公网服务器<br>
     * 2: 内网固定host port型<br>
     * 3: 内网固定host 动态port型<br>
     * 4: 内网可直连
     */
    @SQLField(DEFAULT = true, DEFAULT_text = "0")
    private Integer type;
    /**
     * 服务器连接状态 1:待连接 2:连接中 3:已连接 0:已断开
     */
    @SQLField(DEFAULT = true, DEFAULT_text = "0")
    private Integer status;
    /**
     * 服务器公匙
     */
    @SQLField(DATA_TYPE = "varchar(128)")
    private String public_key;
    /**
     * 服务器私钥
     */
    @SQLField(DATA_TYPE = "varchar(256)")
    private String private_key;
    /**
     * 服务器hash地址
     */
    @SQLField
    private String address;

    private long ping;

    public ServerAttr listener(ServerAttrListener listener) {
        this.listener = listener;
        return this;
    }

    public String link() {
        return host + ":" + port;
    }

    public ServerAttr setId(Integer id) {
        this.id = id;
        listener();
        return this;
    }

    public ServerAttr setName(String name) {
        this.name = name;
        listener();
        return this;
    }

    public ServerAttr setHost(String host) {
        this.host = host;
        listener();
        return this;
    }

    public ServerAttr setPort(Integer port) {
        this.port = port;
        listener();
        return this;
    }

    public ServerAttr setType(Integer type) {
        this.type = type;
        listener();
        return this;
    }

    public ServerAttr setStatus(Integer status) {
        this.status = status;
        listener();
        return this;
    }

    public ServerAttr setPublic_key(String public_key) {
        this.public_key = public_key;
        listener();
        return this;
    }

    public ServerAttr setPrivate_key(String private_key) {
        this.private_key = private_key;
        listener();
        return this;
    }

    public ServerAttr setAddress(String address) {
        this.address = address;
        listener();
        return this;
    }

    private void listener() {
        if (listener != null) {
            listener.listener(this);
        }
    }

    public ServerAttr setPing(long ping) {
        this.ping = ping;
        return this;
    }

    public interface ServerAttrListener {
        void listener(ServerAttr attr);
    }
}
